package cn.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.dao.CompanyDao;
import cn.dao.BanjiDao;
import cn.dao.PostDao;
import cn.dao.StudentDao;
import cn.entity.TCompany;
import cn.entity.TPost;
import cn.entity.TStudent;
import cn.entity.TTeacher;
import cn.entity.TUser;
import cn.util.Const;
import cn.util.PageBean;
@Controller("postAction")
@Scope("prototype")
public class PostAction extends BaseAction{
	private Integer id;
	private String name;
	private TPost post=new TPost();
	private List<TPost> list;
	@Autowired
	@Qualifier("postDao")
	private PostDao postDao;
	@Autowired
	@Qualifier("companyDao")
	private CompanyDao companyDao;
	@Autowired
	@Qualifier("studentDao")
	private StudentDao studentDao;

	private PageBean pageBean=new PageBean();
	
	public String list(){
		String sqlCount="select count(*) from TPost u where 1=1";
		String sql="from TPost u where 1=1";
		if(name!=null && !"".equals(name)){
			sqlCount+=" and u.dangan.student.banji.name like '%"+name+"%'";
			sql+=" and u.dangan.student.banji.name like '%"+name+"%'";
		}
		//毕业生查看自己的投递
		if((Integer)session.getAttribute("role")==1){
			TStudent student=(TStudent)session.getAttribute("currentUser");
			sqlCount+=" and u.dangan.student.id="+student.getId();
			sql+=" and u.dangan.student.id="+student.getId();
		}
		//辅导员查看自己学生的投递
		if((Integer)session.getAttribute("role")==3){
			TTeacher teacher=(TTeacher)session.getAttribute("currentUser");
			sqlCount+=" and u.dangan.student.teacher.id="+teacher.getId();
			sql+=" and u.dangan.student.teacher.id="+teacher.getId();
		}
		sqlCount+=" and u.isdel=0";
		sql+=" and u.isdel=0";
		pageBean.setTotalCount(postDao.getCount(sqlCount));
		list=postDao.getByPage(sql, pageBean.getPageNo(), pageBean.getPageSize());
		return SUCCESS;
	}

	public String add(){
		String uname="无";
		if((Integer)session.getAttribute("role")==2){
			TUser user=(TUser)session.getAttribute("currentUser");
			uname=user.getName();
		}
		if((Integer)session.getAttribute("role")==3){
			TTeacher teacher=(TTeacher)session.getAttribute("currentUser");
			uname=teacher.getName();
		}
		post.setUname(uname);
		post.setIsdel(0);
		
		postDao.add(post);
		return "list";
	}
	
	
	public String detail() throws IOException{
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out=response.getWriter();
		TPost com=postDao.get(TPost.class, id);		
		out.print(JSON.toJSONString(com));
		out.close();
		return null;
	}
	public String toupdate(){
		post=postDao.get(TPost.class, id);
		return "toupdate";
	}
	public String update(){
		TPost j=postDao.get(TPost.class, id);
		if(post.getCompany().getId()!=null){
			j.setCompany(companyDao.get(TCompany.class, post.getCompany().getId()));
		}
		
		if(post.getPosttime()!=null && !"".equals(post.getPosttime())){
			j.setPosttime(post.getPosttime());
		}
		
		postDao.update(j);
		return "list";
	}
	
	
	
	public String delete(){
		TPost s=postDao.get(TPost.class, id);
		s.setIsdel(1);
		postDao.update(s);
		return "list";
	}
	


	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public TPost getPost() {
		return post;
	}

	public void setPost(TPost post) {
		this.post = post;
	}

	public List<TPost> getList() {
		return list;
	}

	public void setList(List<TPost> list) {
		this.list = list;
	}

	public PostDao getPostDao() {
		return postDao;
	}

	public void setPostDao(PostDao postDao) {
		this.postDao = postDao;
	}

	public CompanyDao getCompanyDao() {
		return companyDao;
	}

	public void setCompanyDao(CompanyDao companyDao) {
		this.companyDao = companyDao;
	}

	public StudentDao getStudentDao() {
		return studentDao;
	}

	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}




}
